<?php


class KeValidatorConfigHandler extends sfValidatorConfigHandler
{

 
  protected function generateRegistration($method, &$data, &$methods, &$names, &$validators)
  {
    // setup validator array
    $data[] = "  \$validators = array();";

    if (!isset($methods[$method]))
    {
      $methods[$method] = array();
    }

    // determine which validators we need to create for this request method
    foreach ($methods[$method] as $name)
    {
      if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match))
      {
        // this file/parameter has a parent
        $subname = $match[2];
        $parent  = $match[1];

        $valList = $names[$parent][$subname]['validators'];
      }
      else
      {
        // no parent
        $valList = $names[$name]['validators'];
      }

      if ($valList == null)
      {
        // no validator list for this file/parameter
        continue;
      }

      foreach ($valList as $valName)
      {
        if (isset($validators[$valName]) && !isset($validators[$valName][$method]))
        {
          // retrieve this validator's info
            $validator =& $validators[$valName];
          
            if($validator['class'] == 'sfStringValidator')
            {
                if(strpos($validator['parameters'],  'min_error') === false)
                {
                    $validator['parameters'] = str_replace("'min'", "'min_error'=>'La cadena es demasiado corta.', 'min'" ,$validator['parameters']);
                }
                if(strpos($validator['parameters'],  'max_error') === false)
                {
                    $validator['parameters'] = str_replace("'max'", "'max_error'=>'La cadena es demasiado larga.', 'max'" ,$validator['parameters']);
                }
            }
            if($validator['class'] == 'sfDateValidator')
            {
              /*  if(strpos($validator['parameters'],  'date_error') === false)
                {
                    $validator['parameters'] = str_replace("'min'", "'min_error'=>'La cadena es demasiado corta.', 'min'" ,$validator['parameters']);
                }
                if(strpos($validator['parameters'],  'compare_error') === false)
                {
                    $validator['parameters'] = str_replace("'max'", "'min_error'=>'La cadena es demasiado larga.', 'max'" ,$validator['parameters']);
                }
                */
            }

          $data[] = sprintf("  \$validators['%s'] = new %s();\n".
                            "  \$validators['%s']->initialize(%s, %s);",
                            $valName, $validator['class'], $valName, '$context', $validator['parameters']);

          // mark this validator as created for this request method
          $validators[$valName][$method] = true;
        }
      }
    }

    foreach ($methods[$method] as $name)
    {
      if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match))
      {
        // this file/parameter has a parent
        $subname = $match[2];
        $parent  = $match[1];
        $name    = $match[2];

        $attributes = $names[$parent][$subname];
      }
      else
      {
        // no parent
        $attributes = $names[$name];
      }

      // register file/parameter
      $data[] = sprintf("  \$validatorManager->registerName('%s', %s, %s, %s, %s, %s);",
                        $name, $attributes['required'] ? 1 : 0,
                        isset($attributes['required_msg']) ? $attributes['required_msg'] : "'Campo requerido'",
                        $attributes['parent'], $attributes['group'],
                        $attributes['file']);

      // register validators for this file/parameter
      foreach ($attributes['validators'] as &$validator)
      {
        $data[] = sprintf("  \$validatorManager->registerValidator('%s', %s, %s);", $name,
                          "\$validators['$validator']",
                          $attributes['parent']);
      }
    }

    return count($methods[$method]) ? true : false;
  }

 

  /**
   * Loads all request methods and the file/parameter names that will be
   * validated from the [methods] category.
   *
   * @param string The configuration file name (for exception usage)
   * @param string A request method
   * @param array  An associative array of request method data
   * @param array  An associative array of file/parameter names in which to store loaded information
   * @param array  The loaded ini configuration that we'll use for verification purposes
   * @param string A comma delimited list of file/parameter names
   */
  protected function loadNames(&$configFiles, &$method, &$methods, &$names, &$config, &$list)
  {
    // explode the list of names
    $array = $list;

    // loop through the names
    foreach ($array as $name)
    {
      // make sure we have the required status of this file or parameter
      if (!isset($config['names'][$name]['required']))
      {
        // missing 'required' attribute
        $error = sprintf('Configuration file "%s" specifies file or parameter "%s", but it is missing the "required" attribute', $configFiles[0], $name);
        throw new sfParseException($error);
      }

      // determine parent status
      if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match))
      {
        // this name has a parent
        $subname = $match[2];
        $parent  = $match[1];

        if (!isset($names[$parent]) || !isset($names[$parent][$name]))
        {
          if (!isset($names[$parent]))
          {
            // create our parent
            $names[$parent] = array('_is_parent' => true);
          }

          // create our new name entry
          $entry                 = array();
          $entry['file']         = 'false';
          $entry['group']        = 'null';
          $entry['parent']       = "'$parent'";
          $entry['required']     = 'true';
          $entry['required_msg'] = "'Campo Requerido'";
          $entry['validators']   = array();

          // add our name entry
          $names[$parent][$subname] = $entry;
        }
      }
      else if (strpos($name, '{') !== false || strpos($name, '}') !== false)
      {
        // name contains an invalid character
        // this is most likely a typo where the user forgot to add a brace
        $error = sprintf('Configuration file "%s" specifies method "%s" with invalid file/parameter name "%s"', $configFiles[0], $method, $name);
        throw new sfParseException($error);
      }
      else
      {
        // no parent
        if (!isset($names[$name]))
        {
          // create our new name entry
          $entry                 = array();
          $entry['file']         = 'false';
          $entry['group']        = 'null';
          $entry['parent']       = 'null';
          $entry['required']     = 'true';
          $entry['required_msg'] = "'Campo Requerido'";
          $entry['type']         = 'parameter';
          $entry['validators']   = array();

          // add our name entry
          $names[$name] = $entry;
        }
      }

      // add this name to the current request method
      $methods[$method][] = $name;
    }
  }



 
}
